<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="病人姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入病人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['record:record:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['record:record:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['record:record:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['record:record:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="医生" align="center" prop="staffName" />
      <el-table-column label="病人id" align="center" prop="patientId" width="100"/>
      <el-table-column label="病人" align="center" prop="name" width="100"/>
      <el-table-column label="年龄" align="center" prop="age" width="100"/>
      <el-table-column label="性别" align="center" prop="sex" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="病人病史" align="center" prop="histories" show-overflow-tooltip />-->
      <el-table-column label="症状描述" align="center" prop="symptoms" show-overflow-tooltip />
<!--      <el-table-column label="诊断结果" align="center" prop="diagnosis" show-overflow-tooltip />-->
<!--      <el-table-column label="治疗建议" align="center" prop="treatment" show-overflow-tooltip />-->
      <el-table-column label="创建时间" align="center" prop="createTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['record:record:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['record:record:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDetail(scope.row)"
          >查看详细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改问诊记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="病人ID" prop="patientId">-->
<!--          <el-input v-model="form.patientId" placeholder="请输入病人ID" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="医生ID" prop="doctorId">-->
<!--          <el-input v-model="form.doctorId" placeholder="请输入医生ID" />-->
<!--        </el-form-item>-->
        <el-form-item label="病人id" prop="patientId">
          <el-select v-model="form.patientId" placeholder="请选择" clearable>
            <el-option
              clearable
              v-for="item in userList"
              :key="item.userId"
              :label="`${item.userName} (${item.userId})`"
              :value="item.userId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="医生" prop="doctorId">
          <el-select v-model="form.doctorId" placeholder="请选择" clearable>
            <el-option
              clearable
              v-for="item in staffList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入病人姓名" />
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入病人年龄" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择病人性别">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="病史" prop="histories">
          <el-input v-model="form.histories" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="症状描述" prop="symptoms">
          <el-input v-model="form.symptoms" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="诊断结果" prop="diagnosis">
          <el-input v-model="form.diagnosis" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="治疗建议" prop="treatment">
          <el-input v-model="form.treatment" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看问诊记录详细对话框 -->
    <el-dialog :title="title" :visible.sync="openDetail" width="500px" append-to-body>
      <div>
        <!-- 医生 -->
        <p><b>医生：</b>{{ form.staffName }}</p>
        <el-divider></el-divider>

        <!-- 病人信息 -->
        <p><b>病人id：</b>{{ form.patientId }}</p>
        <div style="display: flex;">
          <p style="flex: 1;"><b>姓名：</b>{{ form.name }}</p>
          <p style="flex: 1;"><b>年龄：</b>{{ form.age }}</p>
          <p style="flex: 1;"><b>性别：</b>
            <span v-if="form.sex === 0">男</span>
            <span v-else-if="form.sex === 1">女</span>
            <span v-else-if="form.sex === 2">未知</span>
          </p>
        </div>

        <el-divider></el-divider>

        <!-- 病史、症状描述 -->
        <p><b>病史：</b>{{ form.histories }}</p>
        <p><b>症状描述：</b>{{ form.symptoms }}</p>
        <el-divider></el-divider>

        <!-- 诊断结果、治疗建议 -->
        <p><b>诊断结果：</b>{{ form.diagnosis }}</p>
        <p><b>治疗建议：</b>{{ form.treatment }}</p>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/record/record";
import { listStaff, getStaff, delStaff, addStaff, updateStaff } from "@/api/staff/staff";
import { listUser, getUser, delUser, addUser, updateUser, resetUserPwd, changeUserStatus, deptTreeSelect } from "@/api/system/user";

export default {
  name: "Record",
  dicts: ['sys_user_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 问诊记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层(新增、修改)
      open: false,
      // 是否显示弹出层(查看详细)
      openDetail: false,
      // 治疗建议时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        patientId: [
          { required: true, message: "病人ID不能为空", trigger: "blur" }
        ],
        doctorId: [
          { required: true, message: "医生ID不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "病人姓名不能为空", trigger: "blur" }
        ],
        age: [
          { required: true, message: "病人年龄不能为空", trigger: "blur" }
        ],
        sex: [
          { required: true, message: "病人性别不能为空", trigger: "change" }
        ],
        symptoms: [
          { required: true, message: "症状描述不能为空", trigger: "blur" }
        ],
        diagnosis: [
          { required: true, message: "诊断结果不能为空", trigger: "blur" }
        ],
        treatment: [
          { required: true, message: "治疗建议不能为空", trigger: "blur" }
        ],
      },
      //医生列表
      staffList: [],
      //病人（用户）列表
      userList: []
    };
  },
  created() {
    this.getList();
    this.getStaffList();
    this.getUserList();
  },
  methods: {
    /** 查询问诊记录列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询员工信息列表 */
    getStaffList() {
      this.loading = true;
      listStaff({
        pageNum: 1,
        pageSize: 100,
      }).then(response => {
        this.staffList = response.rows;
        this.loading = false;
      });
    },
    /** 查询用户列表 */
    getUserList() {
      this.loading = true;
      listUser( {
        pageNum: 1,
        pageSize: 100
      }).then(response => {
          this.userList = response.rows;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openDetail = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        patientId: null,
        doctorId: null,
        name: null,
        age: null,
        sex: null,
        histories: null,
        symptoms: null,
        diagnosis: null,
        treatment: null,
        createTime: null,
        updateTime: null,
        createUser: null,
        updateUser: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加问诊记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改问诊记录";
      });
    },
    /** 查看详细操作 */
    handleDetail(row) {
      this.reset();
      const id = row.id || this.ids
      getRecord(id).then(response => {
        this.form = response.data;
        this.openDetail = true;
        this.title = "问诊记录详细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除问诊记录编号为"' + ids + '"的数据项？').then(function() {
        return delRecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('record/record/export', {
        ...this.queryParams
      }, `record_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
